package com.example.myapplication.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import com.example.myapplication.R;
import com.example.myapplication.tools.ExcelUtils;
import com.example.myapplication.tools.ToastUtils;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelChangeActivity extends Activity {

    String basePath = Environment.getExternalStorageDirectory().getPath() + "/abook/";
    ExcelUtils utils;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_excel_change);
        context = this;
        utils = new ExcelUtils();
        utils.verifyStoragePermissions(this);
    }

    public void evenBtn(View v){
        switch (v.getId()){
            case R.id.excel_read: {
                File file = new File(basePath, "test.xls");
                if (file.exists()) {
                    List<Map<String, String>> list = utils.readExcel(file.getPath());
                    if (list != null) {
                        Gson gson = new Gson();
                        for (Map<String, String> map : list) {
                            Log.e("---------->> ", gson.toJson(map));
                        }
                    }
                } else {
                    ToastUtils.showMessages("test.xls文件不存在", context);
                }
                break;
            }
            case R.id.excel_write:
                File file = new File(basePath,"test_write");
                if(file.exists()){
                    file.delete();
                }
                List<Map<String, String>> list = new ArrayList<>();
                for(int i = 0 ; i < 10 ; i ++){
                    Map<String,String> map = new HashMap<>();
                    map.put("ID", i + "_id");
                    map.put("PC",i + "_pc");
                    map.put("CRC",i + "_crc");
                    map.put("EPC",i + "_epc");
                    list.add(map);
                }
                String[] ids = {"ID","PC","CRC","EPC"};
                utils.writeOperateTagToExcel(file.getPath(),list,ids);
                break;
        }
    }

}